package com.wiz.web.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.wiz.web.dao.mapper.FuellingMapper;
import com.wiz.web.dao.mapper.MemberMapper;
import com.wiz.web.service.DownService;
import com.wiz.web.vo.criteria.AccountAmountHistoryCriteria;
import com.wiz.web.vo.criteria.AccountPointHistoryCriteria;
import com.wiz.web.vo.criteria.FuellingOrderCriteria;
import com.wiz.web.vo.to.AccountAmountHistoryTO;
import com.wiz.web.vo.to.AccountPointHistoryTO;
import com.wiz.web.vo.to.FuellingPayOrderTO;
import com.wiz.web.vo.type.PayChannelType;
import com.wiz.web.vo.type.PointHistoryType;

@Service("downService")
public class DownServiceImpl extends BasicServiceImpl implements DownService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	private final String[] EXCEL_FUELLING_HEADER={"订单号","油品","升数","单价","金额","实收","加油站","时间","支付方式"};
	
	private final String[] EXCEL_ACCOUNT_HEADER={"支付流水号","时间","交易类型","收支类型","金额","余额","备注"};
	
	private final String[] EXCEL_POINT_HEADER={"类型","积分","剩余积分","时间","备注"};
	
	@Resource
	private MemberMapper memberMapper;
	@Resource
	private FuellingMapper fuellingMapper;

	@Override
	public HSSFWorkbook getFuellingOrder(String stationId, String order) {
		FuellingOrderCriteria criteria = gson.fromJson(order, new TypeToken<FuellingOrderCriteria>(){}.getType());
		List<FuellingPayOrderTO> list = fuellingMapper.getFuellingPayOrderList(criteria);
		
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = workbook.createSheet("加油支付订单");
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);
		// 生成一个样式
		HSSFCellStyle style = getTitleStyle(workbook);
		// 产生表格标题行
		HSSFRow row = sheet.createRow(0);
		int columnLength = EXCEL_FUELLING_HEADER.length;
		for (int i = 0; i < columnLength; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(EXCEL_FUELLING_HEADER[i]);
			cell.setCellValue(text);
		}
		HSSFCellStyle styleContent = getContentStyle(workbook);
		for (int rowIndex = 0; rowIndex < list.size(); rowIndex++) {
			row = sheet.createRow(rowIndex + 1);// 第一行被标题占据
			FuellingPayOrderTO to = list.get(rowIndex);
			for (int columnIndex = 0; columnIndex < columnLength; columnIndex++) {
				HSSFCell cell = row.createCell(columnIndex);
				cell.setCellStyle(styleContent);
				if (columnIndex == 0) {
					cell.setCellValue(to.getFuellingOrderId());
				}else if(columnIndex == 1){
					cell.setCellValue(to.getPrName());
				}else if(columnIndex == 2){
					cell.setCellValue(to.getVol());
				}else if(columnIndex == 3){
					cell.setCellValue(to.getPrice());
				}else if(columnIndex == 4){
					cell.setCellValue(to.getReceivableAmount());
				}else if(columnIndex == 5){
					cell.setCellValue(to.getRealincomeAmount());
				}else if(columnIndex == 6){
					cell.setCellValue(to.getStationName());
				}else if(columnIndex == 7){
					SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd hh:mm");
					cell.setCellValue(sdf.format(to.getCreatedTime()));
				}else if (columnIndex == 8) {
					int paidTypeId = to.getPaidTypeId();
					if (paidTypeId == PayChannelType.NONEEDPAY.getId()) {
						cell.setCellValue("无需支付");
					} else if (paidTypeId == PayChannelType.TOBEPAID.getId()) {
						cell.setCellValue("待支付");
					} else if (paidTypeId == PayChannelType.CASH.getId()) {
						cell.setCellValue("现金");
					} else if (paidTypeId == PayChannelType.BALANCE.getId()) {
						cell.setCellValue("余额支付");
					} else if (paidTypeId == PayChannelType.ZHIFUBAO.getId()) {
						cell.setCellValue("支付宝");
					}else if (paidTypeId == PayChannelType.WEIXIN.getId()) {
						cell.setCellValue("微信");
					}
				}
					
			}
		}
		return workbook;
	}

	@Override
	public HSSFWorkbook getAccountHistory(String stationId, String member) {
//		if(!serviceManager.getStationConnectMap().containsKey(stationId)){//申请站点是否在线
//		return returnResult(500,"无法连接油站",null);
//		}
		AccountAmountHistoryCriteria criteria = gson.fromJson(member, new TypeToken<AccountAmountHistoryCriteria>(){}.getType());
		List<AccountAmountHistoryTO> list = memberMapper.getMemberAccountHistroyList(criteria);
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = workbook.createSheet("帐户资金流水");
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);
		// 生成一个样式
		HSSFCellStyle style = getTitleStyle(workbook);
		// 产生表格标题行
		HSSFRow row = sheet.createRow(0);
		int columnLength = EXCEL_ACCOUNT_HEADER.length;
		for (int i = 0; i < columnLength; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(EXCEL_ACCOUNT_HEADER[i]);
			cell.setCellValue(text);
		}
		HSSFCellStyle styleContent = getContentStyle(workbook);
		for (int rowIndex = 0; rowIndex < list.size(); rowIndex++) {
			row = sheet.createRow(rowIndex + 1);// 第一行被标题占据
			AccountAmountHistoryTO to = list.get(rowIndex);
			for (int columnIndex = 0; columnIndex < columnLength; columnIndex++) {
				HSSFCell cell = row.createCell(columnIndex);
				cell.setCellStyle(styleContent);
				if (columnIndex == 0) {
					cell.setCellValue(to.getRefId());
				}else if(columnIndex == 1){
					SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd hh:mm");
					cell.setCellValue(sdf.format(to.getCreatedTime()));
				}else if(columnIndex == 2){
					int paidTypeId = to.getAmountType();
					if (paidTypeId == 1) {
						cell.setCellValue("加油");
					} else if (paidTypeId == 2) {
						cell.setCellValue("充值");
					} else if (paidTypeId == 3) {
						cell.setCellValue("销户");
					}
				}else if(columnIndex == 3){
					int paidTypeId = to.getAmountType();
					if(paidTypeId == 2){
						cell.setCellValue("收入");
					}else{
						cell.setCellValue("支出");
					}
				}else if(columnIndex == 4){
					cell.setCellValue(to.getAmount());
				}else if(columnIndex == 5){
					cell.setCellValue(to.getBalanceAmount());
				}else if(columnIndex == 6){
					cell.setCellValue(to.getMemo());
				}					
			}
		}
		return workbook;
	}

	@Override
	public HSSFWorkbook getPointHistory(String stationId, String member) {
//		if(!serviceManager.getStationConnectMap().containsKey(stationId)){//申请站点是否在线
//		return returnResult(500,"无法连接油站",null);
//		}
		AccountPointHistoryCriteria criteria = gson.fromJson(member, new TypeToken<AccountPointHistoryCriteria>(){}.getType());
		List<AccountPointHistoryTO> list = memberMapper.getMemberPointHistroyList(criteria);
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = workbook.createSheet("帐户资金流水");
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);
		// 生成一个样式
		HSSFCellStyle style = getTitleStyle(workbook);
		// 产生表格标题行
		HSSFRow row = sheet.createRow(0);
		int columnLength = EXCEL_POINT_HEADER.length;
		for (int i = 0; i < columnLength; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			HSSFRichTextString text = new HSSFRichTextString(EXCEL_POINT_HEADER[i]);
			cell.setCellValue(text);
		}
		HSSFCellStyle styleContent = getContentStyle(workbook);
		for (int rowIndex = 0; rowIndex < list.size(); rowIndex++) {
			row = sheet.createRow(rowIndex + 1);// 第一行被标题占据
			AccountPointHistoryTO to = list.get(rowIndex);
			for (int columnIndex = 0; columnIndex < columnLength; columnIndex++) {
				HSSFCell cell = row.createCell(columnIndex);
				cell.setCellStyle(styleContent);
				if(columnIndex == 0){
					int pointTypeId = to.getPointType();
					if (pointTypeId == PointHistoryType.SUB.getId()) {
						cell.setCellValue("兑换");
					} else if (pointTypeId == PointHistoryType.ADD.getId()) {
						cell.setCellValue("加油");
					}
				}else if(columnIndex == 1){
					int pointTypeId = to.getPointType();
					if (pointTypeId == PointHistoryType.SUB.getId()) {
						cell.setCellValue("-"+to.getPoint());
					} else if (pointTypeId == PointHistoryType.ADD.getId()) {
						cell.setCellValue("+"+to.getPoint());
					}
				}else if(columnIndex == 2){
					cell.setCellValue(to.getBalancePoint());
				}else if(columnIndex == 3){
					cell.setCellValue(to.getCreatedTime());
				}else if(columnIndex == 4){
					cell.setCellValue(to.getMemo());
				}				
			}
		}
		return workbook;
	}

	/**
	 * 标题样式
	 * @param workbook
	 * @return
	 */
	private HSSFCellStyle getTitleStyle(HSSFWorkbook workbook){
		// 生成一个样式
		HSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 生成一个字体
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);
		return style;
	}
	
	/**
	 * 内容样式
	 * @param workbook
	 * @return
	 */
	private HSSFCellStyle getContentStyle(HSSFWorkbook workbook){
		HSSFCellStyle style = workbook.createCellStyle();
		style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		HSSFFont font = workbook.createFont();
		font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		style.setFont(font);
		return style;
	}
}
